﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    public class CPPCustomerAppService : AsyncCrudAppService<CPPCustomer, CPPCustomerDto, long, PagedResultRequestDto, CreateCPPCustomerDto, UpdateCPPCustomerDto>, ICPPCustomerAppService
    {
        private readonly IObjectMapper _objectMapper;
        public CPPCustomerAppService(
            IRepository<CPPCustomer, long> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 查询客户信息
        /// </summary>
        /// <param name="skipCount">跳过条数</param>
        /// <param name="maxResultCount">单次最大查询数量</param>
        /// <param name="num">客户名称</param>
        /// <param name="custnum">客户编码</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CPPCustomerDto>> GetList(int skipCount = 0, int maxResultCount = 10, string num = "", string custnum = "")
        {
            PagedResultDto<CPPCustomerDto> resultDto = new PagedResultDto<CPPCustomerDto>();
            List<CPPCustomer> lists = new List<CPPCustomer>();
            Expression<Func<CPPCustomer, bool>> expr = null;
            if (!string.IsNullOrEmpty(num))
            {
                expr = expr == null ? o => o.Name == num : expr.ExpressionAnd(o => o.Name == num);
            }
            if (!string.IsNullOrEmpty(custnum))
            {
                expr = expr == null ? o => o.CustNum == custnum : expr.ExpressionAnd(o => o.CustNum == custnum);
            }
            if (expr != null)
            {
                lists = await Repository.GetAllListAsync(expr);
                var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
                resultDto.Items = _objectMapper.Map<List<CPPCustomerDto>>(dtos);
                resultDto.TotalCount = lists.Count;
            }
            else
            {
                resultDto = await base.GetAllAsync(new PagedResultRequestDto { MaxResultCount = maxResultCount, SkipCount = skipCount });
            }
            return resultDto;
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">id</param>
        /// <returns></returns>
        public async Task BatchDelete(long[] ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                await Repository.DeleteAsync(it=>ids.Contains(it.Id));
            }
        }
        /// <summary>
        /// 根据ID查询客户
        /// </summary>
        /// <param name="customerId">客户Id</param>
        /// <returns></returns>
        public async Task<CPPCustomerDto> GetById(string customerId)
        {
            return _objectMapper.Map<CPPCustomerDto>(await Repository.GetAll().Where(o => o.Id.ToString() == customerId).FirstOrDefaultAsync());
        }
    }
}
